package com.brsya.movie.fragment;

import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.brsya.base.base.BaseMvpFragment;
import com.brsya.base.base.Contents;
import com.brsya.base.bean.InventoryDetailBean;
import com.brsya.base.bean.MovieBean;
import com.brsya.base.recycler.BaseAdapter;
import com.brsya.base.util.GlideUtil;
import com.brsya.base.util.StringUtil;
import com.brsya.base.util.UiUtil;
import com.brsya.movie.R;
import com.brsya.movie.activity.PlayActivity;
import com.brsya.movie.adapter.InventoryDetailListAdapter;
import com.brsya.movie.contract.InventoryDetailContract;
import com.brsya.movie.presenter.InventoryDetailPresenter;

import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/7/7  10:41
 * Description:
 * 片单详情页
 */
public class InventoryDetailFragment extends BaseMvpFragment<InventoryDetailPresenter> implements InventoryDetailContract.View {
    private View vTopBg;
    private ConstraintLayout tlTitleBox;
    private TextView tvPageTitle;
    private LinearLayout llTitleBackBox;
    private SwipeRefreshLayout srlRefresh;
    private RecyclerView rlList;
    private TextView bottomTextView;

    private int titleId;
    private String titleName;
    private int topColor;
    private boolean isBack;
    private View.OnClickListener onBackClickListener;

    private InventoryDetailListAdapter adapter;

    public static InventoryDetailFragment newInstance(String name, int titleId, int topColor, boolean isBack) {

        Bundle args = new Bundle();
        args.putInt("titleId", titleId);
        args.putString("name", name);
        args.putInt("topColor", topColor);
        args.putBoolean("isBack", isBack);

        InventoryDetailFragment fragment = new InventoryDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void setArguments(@Nullable Bundle args) {
        super.setArguments(args);
        if (args != null) {
            isBack = args.getBoolean("isBack", false);
            titleId = args.getInt("titleId", 1);
            topColor = args.getInt("topColor", 0xFF375C3D);
            titleName = args.getString("name");
        }
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_inventory_detail);

        vTopBg = findViewById(R.id.v_top_bg);
        tlTitleBox = findViewById(R.id.tl_title_box);
        tvPageTitle = findViewById(R.id.tv_page_title);
        llTitleBackBox = findViewById(R.id.ll_title_back_box);
        srlRefresh = findViewById(R.id.srl_refresh);
        rlList = findViewById(R.id.rl_list);
    }

    @Override
    protected void initData() {
        UiUtil.setStatusPadding(getContext(), tlTitleBox);
        if (!StringUtil.isEmpty(titleName)) {
            tvPageTitle.setText(titleName);
        }
        tlTitleBox.setVisibility(isBack ? View.VISIBLE : View.GONE);

        if (onBackClickListener != null) {
            llTitleBackBox.setOnClickListener(onBackClickListener);
        }


        int[] colors = {topColor, 0x00FFFFFF};
        GradientDrawable bg = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
        vTopBg.setBackground(bg);

        srlRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.getInventoryDetail(titleId);
            }
        });

        rlList.setLayoutManager(new LinearLayoutManager(getContext()));
        rlList.setAdapter(adapter = new InventoryDetailListAdapter(getContext()));

        View bottomView = LayoutInflater.from(getContext()).inflate(R.layout.item_no_more_data, null, false);
        bottomTextView = bottomView.findViewById(R.id.tv_no_more_data_text);
        adapter.setBottomView(bottomView);
        bottomTextView.setVisibility(View.GONE);

        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<InventoryDetailBean>() {
            @Override
            public void onItemClick(int position, InventoryDetailBean data) {
                Intent intent = new Intent(getActivity(), PlayActivity.class);
                MovieBean movieBean = new MovieBean();
                movieBean.setId(data.getId());
                movieBean.setName(data.getName());
                movieBean.setCoverUrl(data.getCoverUrl());
                movieBean.setUpState(data.getUpState());
                movieBean.setDoubanScore(data.getDoubanScore());
                movieBean.setVideoArea(data.getVideoArea());
                movieBean.setVideoYear(data.getVideoYear());
                movieBean.setVideoTag(data.getVideoTag());
                intent.putExtra(Contents.INTENT_MOVIE_DETAIL, movieBean);
                startActivity(intent);
            }
        });

        presenter.getInventoryDetail(titleId);
    }

    @Override
    protected void initPresenter() {
        presenter = new InventoryDetailPresenter();
        presenter.attachView(this);
    }

    @Override
    public void showInventoryList(List<InventoryDetailBean> inventoryList) {
        if (inventoryList == null || inventoryList.size() == 0) {
            noData();
            return;
        }

        if (topColor == 0xFF375C3D) {

            if (inventoryList.get(0) != null && !StringUtil.isEmpty(inventoryList.get(0).getCoverUrl())) {
                GlideUtil.loadBitmapColor(getContext(), inventoryList.get(0).getCoverUrl(), new GlideUtil.OnBitmapColorListener() {
                    @Override
                    public void onBitmapColor(int color) {
                        // 图片变暗
                        color = color & 0xFFAAAAAA;
                        int[] colors = {color, 0x00FFFFFF};
                        GradientDrawable bg = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
                        vTopBg.setBackground(bg);
                    }
                });
            }
        }
        adapter.setDataList(inventoryList);
        bottomTextView.setVisibility(View.VISIBLE);
    }

    @Override
    public void noData() {
        bottomTextView.setVisibility(View.VISIBLE);
        bottomTextView.setText("数据为空");
    }

    /**
     * 设置点击返回键回调
     *
     * @param clickListener 点击回调
     */
    public void setOnBackListener(View.OnClickListener clickListener) {
        this.onBackClickListener = clickListener;
        if (onBackClickListener != null && llTitleBackBox != null) {
            llTitleBackBox.setOnClickListener(onBackClickListener);
        }
    }

    @Override
    public void showLoading() {
        super.showLoading();
        srlRefresh.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        srlRefresh.setRefreshing(false);
    }
}
